# 导入MySQL驱动:
import mysql.connector
# 注意把password设为你的root口令:
conn = mysql.connector.connect(user='demo', password='123456', host='127.0.0.1', port='3306', database='demo')
cursor = conn.cursor()
# 创建user表:
cursor.execute('create table user (user_id varchar(20) primary key, user_name varchar(20))')
# 插入一行记录，注意MySQL的占位符是%s:
cursor.execute('insert into user (user_id, user_name) values (%s, %s)', ['123', '我是第一条数据'])
print(cursor.rowcount)

# 提交事务:
conn.commit()
cursor.close()

# 运行查询:
cursor = conn.cursor()
cursor.execute('select * from user where user_id = %s', ('123',))
values = cursor.fetchall()
print(values)

# 关闭Cursor
cursor.close()
# 关闭连接
conn.close()

